﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.Common;

namespace AcctrueWMS.Foundation.DAL
{
    public class DBFactory
    {
        /// <summary>
        /// 从config中获取数据库类型
        /// </summary>
        private static readonly String dbType = ConfigurationManager.AppSettings["DbType"].ToLower();
        /// <summary>
        /// 从config中获取数据库连接字符串
        /// </summary>
        private static readonly String connString = ConfigurationManager.ConnectionStrings["WMSDB"].ToString();
        static DBFactory()
        {
        }
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string ConnString
        {
            get
            {
                return connString;
            }
        }
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static string DbType
        {
            get
            {
                return dbType;
            }
        }

        /// <summary>
        /// 功能：根据不同数据库类型创建不同数据操作对象
        /// </summary>
        /// <returns>返回统一数据库操作对象</returns>
        public static IDBOperator CreateDBOperator()
        {
            switch (dbType)
            {
                case DBTypeConst.ORACLE:
                    return new OracleOperator();
                case DBTypeConst.MSSQL:
                    return new SqlOpeartor();
                default:
                    return new SqlOpeartor();
            }
        }

        /// <summary>
        /// 功能：根据系统配置，返回对应连接
        /// </summary>
        /// <returns>返回与数据库匹配的数据库连接对象</returns>
        public static IDbConnection NewConnection()
        {
            return NewConnection(connString);
        }

        /// <summary>
        ///功能：生成 适配器
        /// </summary>
        /// <param name="cmd">.net Command</param>
        /// <returns>返回 DbDataAdapter 对象</returns>
        public static DbDataAdapter NewDataAdapter(DbCommand cmd)
        {
            switch (DbType)
            {
                case DBTypeConst.ORACLE:
                    return new OracleDataAdapter(cmd as OracleCommand);
                default:
                    return new SqlDataAdapter(cmd as SqlCommand);
            }
        }

        /// <summary>
        /// 功能：处理参数化查询时 oracle 与 sqlserver 语法差异 sqlserver 中变量定义是@  oracle 中变量定义是:
        /// </summary>
        /// <param name="queryStr">查询字符串</param>
        /// <returns>返回与数据库类型匹配的查询字符串</returns>
        public static string NewQueryStr(string queryStr)
        {
            switch (dbType)
            {
                case DBTypeConst.ORACLE:
                    return queryStr.Replace("@", ":");
                default:
                    return queryStr;
            }
        }

        /// <summary>
        /// 功能：生成参数对象
        /// </summary>
        /// <param name="name">参数名</param>
        /// <param name="value">值</param>
        /// <returns>返回参数对象</returns>
        public static IDataParameter NewParameter(string name, object value)
        {
            switch (dbType)
            {
                case DBTypeConst.ORACLE:
                    return new OracleParameter(name.Replace("@", ":"), value);
                case DBTypeConst.MSSQL:
                    return new SqlParameter(name, value);
                default:
                    return new SqlParameter(name, value);
            }
        }

        /// <summary>
        /// 功能：生成数据库连接对象
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <returns>返回数据库连接对象</returns>
        public static IDbConnection NewConnection(string connString)
        {
            switch (dbType)
            {
                case DBTypeConst.ORACLE:
                    return new OracleConnection(connString);
                case DBTypeConst.MSSQL:
                    return new SqlConnection(connString);
                default:
                    return new SqlConnection(connString);
            }
        }

        /// <summary>
        /// 功能： 将参数数组转换为本地数据库参数数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static T[] ToRealParamters<T>(IDataParameter[] parameters)
        {
            if (parameters == null)
                return null;
            T[] realParams = new T[parameters.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                realParams[i] = (T)parameters[i];
            }
            return realParams;
        }

        /// <summary>
        ///  功能：构造.net commond
        /// </summary>
        /// <returns>.net commond 对象</returns>
        public static DbCommand NewCommand()
        {
            switch (dbType)
            {
                case DBTypeConst.ORACLE:
                    return new OracleCommand();
                case DBTypeConst.MSSQL:
                    return new SqlCommand();
                default:
                    return new SqlCommand();
            }
        }
        /// <summary>
        /// 构造.net commond
        /// </summary>
        /// <param name="cmdText">执行sql 语句</param>
        /// <param name="con">数据库连接对象</param>
        /// <returns>.net commond 对象</returns>
        public static DbCommand NewCommand(string cmdText,IDbConnection con)
        {
            switch (dbType)
            {
                case DBTypeConst.ORACLE:
                    return new OracleCommand(cmdText,con as OracleConnection);
                default:
                    return new SqlCommand(cmdText,con as SqlConnection);
            }
        }
    }
}
